<template>
  <div class="live-view">
    <!-- 顶部横幅 -->
    <div class="live-header">
      <h1>直播间</h1>
      <p>发现精彩直播内容</p>
    </div>

    <!-- 分类导航 -->
    <div class="live-categories">
      <div 
        v-for="category in categories" 
        :key="category.id"
        :class="['category-item', { active: currentCategory === category.id }]"
        @click="switchCategory(category.id)"
      >
        <span class="category-icon">{{ category.icon }}</span>
        <span class="category-name">{{ category.name }}</span>
      </div>
    </div>

    <!-- 直播列表 -->
    <div class="live-rooms">
      <div class="live-grid">
        <div v-for="room in liveRooms" :key="room.id" class="live-room-card">
          <router-link :to="`/live/${room.id}`" class="live-room-link">
            <div class="live-cover">
              <img :src="room.cover" :alt="room.title" class="cover-image" />
              <div v-if="room.status === 'live'" class="live-badge">
                <span class="live-dot"></span>
                <span>直播中</span>
              </div>
              <div class="viewer-count">
                <i class="el-icon-user"></i>
                <span>{{ formatViewerCount(room.viewers) }}</span>
              </div>
              <div class="live-duration">
                {{ room.startTime ? formatLiveDuration(room.startTime) : '' }}
              </div>
            </div>
            <div class="live-info">
              <div class="streamer-info">
                <img :src="room.streamer.avatar" :alt="room.streamer.name" class="streamer-avatar" />
                <div class="streamer-name">{{ room.streamer.name }}</div>
              </div>
              <div class="live-title">{{ room.title }}</div>
              <div class="live-tags">
                <span v-for="(tag, index) in room.tags" :key="index" class="live-tag">
                  {{ tag }}
                </span>
              </div>
            </div>
          </router-link>
        </div>
      </div>

      <!-- 加载更多 -->
      <div class="load-more">
        <el-button 
          type="primary" 
          :loading="loading" 
          @click="loadMore"
          :disabled="!hasMore"
        >
          {{ hasMore ? '加载更多' : '没有更多了' }}
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { liveService } from '../services'

export default {
  name: 'LiveView',
  data() {
    return {
      liveRooms: [],
      categories: [],
      currentCategory: 'all',
      page: 1,
      limit: 20,
      totalPages: 1,
      loading: false
    }
  },
  computed: {
    hasMore() {
      return this.page < this.totalPages
    }
  },
  async mounted() {
    await this.fetchCategories()
    await this.fetchLiveRooms()
  },
  methods: {
    async fetchCategories() {
      try {
        this.categories = await liveService.getLiveCategories()
      } catch (error) {
        this.$message.error('获取分类失败')
      }
    },
    async fetchLiveRooms() {
      this.loading = true
      try {
        const category = this.currentCategory === 'all' ? null : this.currentCategory
        const result = await liveService.getLiveRoomsByCategory(category, this.page, this.limit)
        
        if (this.page === 1) {
          this.liveRooms = result.rooms
        } else {
          this.liveRooms = [...this.liveRooms, ...result.rooms]
        }
        
        this.totalPages = result.totalPages
      } catch (error) {
        this.$message.error('获取直播间失败')
      } finally {
        this.loading = false
      }
    },
    async switchCategory(categoryId) {
      this.currentCategory = categoryId
      this.page = 1
      await this.fetchLiveRooms()
    },
    async loadMore() {
      if (this.hasMore) {
        this.page++
        await this.fetchLiveRooms()
      }
    },
    formatViewerCount(viewers) {
      if (viewers >= 10000) {
        return (viewers / 10000).toFixed(1) + '万'
      }
      return viewers
    },
    formatLiveDuration(startTime) {
      const start = new Date(startTime)
      const now = new Date()
      const diff = now - start
      const hours = Math.floor(diff / (1000 * 60 * 60))
      const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
      
      if (hours > 0) {
        return `${hours}小时${minutes}分钟`
      }
      return `${minutes}分钟`
    }
  }
}
</script>

<style scoped>
.live-view {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.live-header {
  text-align: center;
  margin-bottom: 30px;
}

.live-header h1 {
  font-size: 36px;
  color: #1890ff;
  margin-bottom: 10px;
}

.live-header p {
  font-size: 16px;
  color: #666;
}

.live-categories {
  display: flex;
  overflow-x: auto;
  padding: 10px 0;
  margin-bottom: 30px;
  gap: 15px;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.live-categories::-webkit-scrollbar {
  display: none;
}

.category-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: #f5f5f5;
  border-radius: 20px;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.3s;
}

.category-item:hover {
  background: #e6f7ff;
  color: #1890ff;
}

.category-item.active {
  background: #1890ff;
  color: white;
}

.category-icon {
  font-size: 18px;
}

.live-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.live-room-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
}

.live-room-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.live-room-link {
  display: block;
  text-decoration: none;
  color: inherit;
}

.live-cover {
  position: relative;
  padding-top: 56.25%;
  background: #f0f0f0;
}

.cover-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.live-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 4px 8px;
  background: rgba(255, 0, 0, 0.8);
  color: white;
  border-radius: 4px;
  font-size: 12px;
}

.live-dot {
  width: 8px;
  height: 8px;
  background: white;
  border-radius: 50%;
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
  100% {
    opacity: 1;
  }
}

.viewer-count {
  position: absolute;
  bottom: 10px;
  right: 10px;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 4px 8px;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  border-radius: 4px;
  font-size: 12px;
}

.live-duration {
  position: absolute;
  bottom: 10px;
  left: 10px;
  padding: 4px 8px;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  border-radius: 4px;
  font-size: 12px;
}

.live-info {
  padding: 12px;
}

.streamer-info {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.streamer-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
}

.streamer-name {
  font-size: 14px;
  color: #666;
}

.live-title {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  height: 40px;
}

.live-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.live-tag {
  padding: 2px 8px;
  background: #f0f0f0;
  border-radius: 10px;
  font-size: 12px;
  color: #666;
}

.load-more {
  text-align: center;
  margin-top: 20px;
}

@media (max-width: 768px) {
  .live-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .live-header h1 {
    font-size: 28px;
  }
}

@media (max-width: 480px) {
  .live-grid {
    grid-template-columns: 1fr;
  }
}
</style>